package net.xi.news.dao;

import net.xi.news.pojo.News;
import net.xi.news.pojo.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface NewsRespository extends JpaRepository<News,Long>, JpaSpecificationExecutor<News> {

    @Query("select n from News n where n.recommend = true")
    List<News> findTop(Pageable pageable);

    @Query("select n from News n where n.title like ?1 or n.content like ?1")
    Page<News> findByQuery(String query,Pageable pageable);


    @Query("select function('date_format',n.updateTime,'%M') as month from News n group by function('date_format',n.updateTime,'%M') order by month DESC ")
    List<String> findGroupBy();


    @Transactional
    @Modifying
    @Query("update News n set n.views = n.views+1 where n.id = ?1")
    int updateViews(Long id);


    @Query("select n from News n where function('date_format',n.updateTime,'%M') = ?1")
    List<News> findByMonth(String month);

}
